package org.wells.dlock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.curator.framework.CuratorFramework;
import org.wells.dlock.zk.pool.ZkConnectionFactory;
import org.wells.dlock.zk.pool.ZkLock;

public class DLock implements IDLock {
	private String lockName;
	private ZkLock zkLock;

	private Lock localLock = new ReentrantLock();

	public DLock(String zkAddress, String lockName) {
		super();
		this.lockName = lockName;
		this.zkLock = new ZkLock(new GenericObjectPool<CuratorFramework>(new ZkConnectionFactory(zkAddress, 1000, 3)));
	}

	public void lock() {

		localLock.lock();// 本地锁

		try {
			zkLock.lock(lockName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public IDLock getParticleLock(String particle) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean tryLock() {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean tryLock(long time, TimeUnit unit) {
		// TODO Auto-generated method stub
		return false;
	}

	public void unlock() {
		zkLock.unlock();

		localLock.unlock();

	}

}
